package example.charlyzhang.jnicalljava;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;

import example.charlyzhang.com.jnicalljava.R;

public class MainActivity extends AppCompatActivity {

    static AssetManager assets = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView)this.findViewById(R.id.tv);

        tv.setText( stringFromJNI() );

        ImageView iv = (ImageView)this.findViewById(R.id.iv);
        Bitmap bitmap = null;
//        bitmap = getImageFromAssets();
//        bitmap = getImageFromSD();

        String path= Environment.getExternalStorageDirectory().getAbsolutePath() + "/ObjViewer/data/南禅寺1/wapian.png";
        bitmap = getImage(path);

        iv.setImageBitmap(bitmap);
    }

    public native String stringFromJNI();

    public native Bitmap getImage(String path);

    static {
        System.loadLibrary("native");
    }

    public static Bitmap getImageFromAssets() {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(assets.open("star.png"));
        } catch (IOException e) {
        }

        return bitmap;
    }

    public static Bitmap getImageFromSD() {
        Bitmap bitmap = null;

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String path= Environment.getExternalStorageDirectory().getAbsolutePath() + "/ObjViewer/data/南禅寺1/mu.png";

            File mFile=new File(path);
            if (mFile.exists()) {
                bitmap=BitmapFactory.decodeFile(path);
            }
        }

        return bitmap;
    }

}
